package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


@RestController
@Slf4j
public class UploadController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")


    //在Spring框架的控制器方法中，参数 (MultipartFile file)表示这个方法接收一个通过HTTP请求上传的文件
    // MultipartFile 多部分文件
     public Result upload(MultipartFile file) throws Exception{

        log.info("上传文件:{}",file);
        if (!file.isEmpty()) {
            //生成唯一的文件名
            String originalFilename = file.getOriginalFilename();
            //上传文件
            String url = aliyunOSSOperator.upload(file.getBytes(), originalFilename);
            return Result.success(url);
        }
        return Result.error("上传失败");
    }
}
